<?php
// No direct access
defined ('_JEXEC') or die();

jimport ('joomla.application.component.modellist');

class ShipnhanhModelTracking extends JModelList
{
	public function __construct($config = array())
	{
		$config['filter_fields'] = array('id', 'status', 'orderdate', 'productname');
		
		parent::__construct($config);
	}
	
	protected function getListQuery()
	{
		$user = JFactory::getUser();
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		
		$query->select('o.id, s.description AS status, o.orderdate, o.productname, o.customer, o.photo, o.quantity, o.vndate');
		$query->from('#__sn_orders AS o');
		$query->join('INNER','#__sn_order_status AS s ON (s.id = o.status)');
		$query->where('userid=' . $user->id);
		
		// Filter search
		$search = $this->getState('filter.search');
		
		if (!empty($search))
		{
			$where = '';
			
			if (is_numeric($search))
			{
				$where .= 'o.id=' . $db->getEscaped($search) . ' OR';
			}
			$where .= ' o.customer LIKE ' . $db->quote('%' . $search . '%') . ' OR o.productname LIKE ' . $db->quote('%' . $search . '%');
			
			$query->where($where);
		}
		
		// Filter status
		$status = $this->getState('filter.status');
		if (!empty($status))
		{
			$query->where('o.status=' . $status);
		}
		
		$query->order($this->getState('list.ordering', 'o.id') .
				' ' . $this->getState('list.direction', 'ASC'));
		
		return $query;
	}
	
	protected function populateState($ordering = null, $direction = null)
	{
		// Load the filter state
		$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
		$this->setState('filter.search', $search);
		
		//Filter (dropdown) order status
		$state = $this->getUserStateFromRequest($this->context.'.filter.status', 'filter_status', '', 'string');
		$this->setState('filter.status', $state);
		
		parent::populateState($ordering, $direction);
	}
}